$(function(){
	//jQuery.support.cors = true; /*解决ie8/9跨域问题*/
	getTopNav();
	getLeftNotice('getNoticeList', 6);
	loadLeftNav('getDepartmentList', 26);
	loadLeftNav('getLeftMenulist');
	
	//让ie8兼容map方法
	if (typeof Array.prototype.map != "function") {
	  Array.prototype.map = function (fn, context) {
	    var arr = [];
	    if (typeof fn === "function") {
	      for (var k = 0, length = this.length; k < length; k++) {      
	         arr.push(fn.call(context, this[k], k, this));
	      }
	    }
	    return arr;
	  };
	} 
	
})
			
var pageType = ''; //页面类型
			
/*===========================获取顶部导航===================================*/
			
function getTopNav(){
	$.ajax({
		type: "get", 
		url: server+pageName+"getTopMenulist",
		dataType: "json",
		async: true,
		contentType: "application/json",
		data: {length: 13},
		success: function(data){
			if(data.resultCode == 0){
				for (var i=0; i<data.resultData.length; i++) {
					var $li = $('<li></li>');
					var $a = '';
					if(data.resultData[i].childrenList.length){
						$a = $('<a onclick="loadPage('+"'"+data.resultData[i].pageType+"'"+', '+"'"+data.resultData[i].value+"'"+')">'+data.resultData[i].name+'</a>');
						var str = '<dl class="nav-item-child">';
								for (var j=0; j<data.resultData[i].childrenList.length; j++) {
									pageId = data.resultData[i].childrenList[j].value;  //页面id
									pageType = data.resultData[i].childrenList[j].pageType;  //页面类型
									str += '<dd><a style="font-size:14px;" onclick="loadPage('+"'"+pageType+"'"+', '+"'"+pageId+"'"+')">'+data.resultData[i].childrenList[j].name+'</a></dd>';
								}
								str += '</dl>';
						$li.append($a);
						$li.append(str);
					}else{
						pageId = data.resultData[i].value;
						pageType = data.resultData[i].pageType;
						$a = $('<a onclick="loadPage('+"'"+pageType+"'"+', '+"'"+pageId+"'"+')">'+data.resultData[i].name+'</a>');
						$li.append($a);
					}
	
					if(i == 0){
						$li.addClass('current');
					}
					$('#navList').append($li);
				}
				
				$('.nav > ul li a').click(function(){
					$('.nav > ul li').removeClass('current');
					$(this).parent('li').addClass('current');
				}) 
				
			}	
		},
		error: function(err){
			console.log(err);
		}
	})
}

//点击切换导航
function loadPage(pageType, pageId, pageSize, pageNo) {
	$('#main').show();
	$('#form').hide();
	
	var pageSize = pageSize == undefined || pageSize == '' ? 15 : pageSize,
	    pageNo = pageNo == undefined || pageNo == '' ? 1 : pageNo;
			//pageId = "id="+pageId;
	
	if(pageType == 'home'){ //首页
		toHome();
		
	}else if(pageType == 'list'){  //列表页
		if(pageId == 'news'){
			toList('/listPage/getNewsPage', '', '', pageSize, pageNo);
			
		}else if(pageId == 'notice') {
			toList('/listPage/getNoticePage', '', '', pageSize, pageNo);
			
		}else if(pageId == 'export') {
			toList('/listPage/getAllDepartmentExpertList', '', '', pageSize, pageNo);
			
		}else{
			toList('/listPage/getModelInfoPage', '',pageId, pageSize, pageNo);
		}
		
	}else if(pageType == 'details'){  //详情页
		if(pageId == '25803fe6a4064c21b0252c4225bc1161'){
			pageId = 'bd3964322d3f40d285de655d6a316992';
		}
		toDetails(pageId);
	}
	
}



/*===========================获取左侧数据===================================*/

//医院公告
function getLeftNotice(requestUrl, len) {
	$.ajax({
		type:"get",
		url: server+pageName+requestUrl,
		dataType:"json",
		async:true,
		data: {length: len},
		success: function(data){
			if(data.resultCode == 0){
				var getData = data.resultData.dataList;
				showNoticeList(getData);
			}
		},
		error: function(err){
			console.log(err)
		}
	})
}

function showNoticeList(arr){
	arr.map(function(item, index){
		var pageId = item.value;
		var $a = $('<a onclick="toDetails('+"'"+pageId+"'"+')">'+item.title+'</a>');
		var $li = $('<li></li>');
		$li.append($a);
		$('#noticeList').append($li);
	})
	$('#noticeList').removeClass('layui-icon-loading');
}


function loadLeftNav(requestUrl, len){
	$.ajax({
		type:"get",
		url: server+pageName+requestUrl,
		dataType:"json",
		async:true,
		data: {length: len},
		success: function(data){
			if(data.resultCode == 0){
				var getData = data.resultData.dataList;
				if(requestUrl == 'getDepartmentList'){
					showDepartment(getData);
				}
			  
			  if(requestUrl == 'getLeftMenulist'){
					showLeftMenu(data.resultData);
				}
		  }
		},
		error: function(err){
			console.log('请求出错')
		}
  });
}

//获取科室简介列表
function showDepartment(arr){
	arr.map(function(item, index){
		var pageId = item.value;
		var $a = '<a onclick="toDetails('+"'"+pageId+"'"+')">'+item.name+'</a>'; 
		$('#departmentList').append($a);
	})
	
	$('#departmentList a').click(function(){
		$('.nav > ul li').removeClass('current').eq(0).addClass('current');
	})
}

function showLeftMenu(arr){
	arr.map(function(item, index){
		//console.log(item)
		var pageId = item.value;
		var $a = '';
		if(item.name == '医院信箱' || item.name == '院长信箱'){
			$a = $('<a onclick="toForm()">'+item.name+'</a>');
			
		}else if(item.name == '下载专区'){
			$a = $('<a onclick="toDownLoad()">'+item.name+'</a>');
			
		}else if(item.name == '新闻搜索'){
			$a = $('<a onclick="toSearch()">'+item.name+'</a>');
			
		}else if(item.name == '预约挂号'){
			$a = $('<a href="https://web.bshcn.com.cn/jyb-download/index.html" target="_blank">'+item.name+'</a>');
			
		}else if(item.pageType == 'list'){
			var url = "'"+'/listPage/getModelInfoPage'+"',";
			$a = $('<a onclick="toList('+url+"'"+"',"+"'"+pageId+"'"+', 15, 1)">'+item.name+'</a>');
			
		}else if(item.pageType == 'details'){
			$a = $('<a onclick="toDetails('+"'"+pageId+"'"+', false)">'+item.name+'</a>');
		}

		$('#leftMenu').append($a);
	})
	
	$('#leftMenu a').click(function(){
		$('.nav > ul li').removeClass('current').eq(0).addClass('current');
	})
	
}




	
